<template>
  <Modal v-model="modal" :title="title" width="360">
    <p slot="header" >
        <Icon type="ios-information-circle"></Icon>
        <span>批量设置优惠券</span>
    </p>
    <Form :model="form" :label-width="60" ref="form">
      <FormItem :label-width="labelWidth">
        <Select v-model="form.couponId" clearable filterable filter-by-label @on-change="onChoiceCoupon">
          <Option
            v-for="(item, index) in bargainCouponsData"
            :value="item.value"
            :key="index"
            :label="item.label"
          >{{ item.label }}</Option>
        </Select>
      </FormItem>
    </Form>
    <div slot="footer">
      <Button  size="large" type="text"  @click="onAgreeCancel">取消</Button>
      <Button type="primary" size="large"   @click="onAgree">确认</Button>
    </div>
  </Modal>
</template>
<script>
import { mapGetters } from 'vuex'
export default {
  props: {
    modal: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      id: this.$route.query.id || '',
      list: [{
        value: 1,
        label: '减1'
      }],
      title: '',
      form: {
        couponId: ''
      },
      labelWidth: '100px',
      dataType: ''
    }
  },
  mounted() {
    // !this.bargainCouponsData.length && this.$store.dispatch('getCouponsData')
  },
  methods: {
    onChoiceCoupon() {

    },
    reset(row) {
      this.modal = true
      this.dataType = row.type
      // this.form.couponId = null
    },
    onAgree() {
      let ids = []
      let data = []
      if (this.dataType == '1') {
        data = this.$store.state.bargain.getBgLmtData
      } else if (this.dataType == '2') {
        data = this.$store.state.bargain.getBgUnLmtData
      }
      for (let item of data) {
        for(let todo of item.productSkuList) {
          if (todo.checked) ids.push(todo.acId)
        }
      }
      if (!ids.length && (this.dataType == '1' || this.dataType == '2')) {
        this.$Notice.warning({
          title: '温馨提示',
          desc: "请勾选相应的想要设置商品"
        })
        this.modal = false
        return
      }
      if (!this.form.couponId && this.form.couponId != '0') {
        this.$Notice.warning({
          title: '温馨提示',
          desc: "请选择优惠券"
        })
        return
      }
      this.modal = false
      if (this.dataType == '0') {
        this.$store.commit('onBargainBatchSelect', {id: this.form.couponId == '0' ? '' : this.form.couponId, dataType: this.dataType})
      }else {
        this.$store.dispatch('setCouponId', {activityId: this.id, ids: ids, giftCouponId: this.form.couponId == '0' ? '' : this.form.couponId}).then(res => {
          this.$Notice.success({
            title: '温馨提示',
            desc: res + "条优惠券修改成功" 
          })
          this.$store.commit('onBargainBatchSelect', {id: this.form.couponId == '0' ? '' : this.form.couponId, dataType: this.dataType})
        })
      }
    },
    onAgreeCancel() {
      this.modal = false
    }
  },
  computed: {
    ...mapGetters([
      'bargainCouponsData'
    ])
  }
}
</script>

